package cn.jiedh.app.meiweidk.loan;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import cn.jiedh.app.meiweidk.R;
import cn.jiedh.app.meiweidk.Utils.tool.HtmlUtil;
import cn.jiedh.app.meiweidk.base.BaseFragment;
import cn.jiedh.app.meiweidk.baseadapter.BaseAdapter;
import cn.jiedh.app.meiweidk.baseadapter.BaseViewHolder;
import cn.jiedh.app.meiweidk.entity.LoanDetailBean;

import butterknife.BindView;

/**
 * @name：产品详情tab页
 * @author：司马林
 * @phone：18423134135
 * @createTime：2017/10/23
 */

public class LoanDetailInfoFragment extends BaseFragment {
    @BindView(R.id.condition_rv)
    RecyclerView conditionRv;
    @BindView(R.id.guide_rv)
    RecyclerView guideRv;
    @BindView(R.id.process_web)
    WebView processWeb;
    @BindView(R.id.crowd_oriented_tv)
    TextView crowdOrientedTv;
    @BindView(R.id.audit_mode_tv)
    TextView auditModeTv;
    @BindView(R.id.way_to_arrival_tv)
    TextView wayToArrivalTv;
    @BindView(R.id.service_charge_tv)
    TextView serviceChargeTv;
    @BindView(R.id.actual_arrival_tv)
    TextView actualArrivalTv;
    @BindView(R.id.repayment_methods_tv)
    TextView repaymentMethodsTv;
    @BindView(R.id.is_prepayment_tv)
    TextView isPrepaymentTv;
    @BindView(R.id.overdue_algorithm_tv)
    TextView overdueAlgorithmTv;
    @BindView(R.id.is_check_the_credit_tv)
    TextView isCheckTheCreditTv;
    @BindView(R.id.is_raise_or_not_tv)
    TextView isRaiseOrNotTv;


    private BaseAdapter<String> conditionAdapter;
    private BaseAdapter<String> guideAdapter;

    private LoanDetailBean bean;

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_loan_detail_info;
    }

    @Override
    protected void initView(View view) {
        if (getArguments() != null) {
            bean = (LoanDetailBean) getArguments().getSerializable("detail");
        }

        LinearLayoutManager conditionLayoutManager = new LinearLayoutManager(getActivity()) {
            //禁止rv滑动
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        LinearLayoutManager guideLayoutManager = new LinearLayoutManager(getActivity()) {
            //禁止rv滑动
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        conditionRv.setLayoutManager(conditionLayoutManager);
        guideRv.setLayoutManager(guideLayoutManager);

        conditionAdapter = new BaseAdapter<String>(getActivity(), R.layout.loan_detail_info_item, bean.getApplyCondition()) {
            @Override
            public void convert(BaseViewHolder holder, String s, int position) {
                holder.setText(R.id.detail_tv, s);
            }
        };
        conditionRv.setAdapter(conditionAdapter);

        guideAdapter = new BaseAdapter<String>(getActivity(), R.layout.loan_detail_info_item, bean.getNewGuidance()) {
            @Override
            public void convert(BaseViewHolder holder, String s, int position) {
                holder.setText(R.id.detail_tv, s);
            }
        };
        guideRv.setAdapter(guideAdapter);

        WebSettings settings = processWeb.getSettings();
        settings.setAppCacheEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setDatabaseEnabled(true);
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        settings.setJavaScriptEnabled(true);
        settings.setLoadWithOverviewMode(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setSupportZoom(true);
        processWeb.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

        processWeb.loadData(bean.getAppProcess(), HtmlUtil.MIME_TYPE, HtmlUtil.ENCODING);

        crowdOrientedTv.setText("面向人群：" + bean.getPeopleType());
        auditModeTv.setText("审核方式：" + bean.getExamineType());
        wayToArrivalTv.setText("到账方式：" + bean.getWayArrival());
        serviceChargeTv.setText("服务费用：" + bean.getServiceCharge());
        actualArrivalTv.setText("实际到账：" + bean.getActualArrival());
        repaymentMethodsTv.setText("还款方式：" + bean.getRepaymentMethods());
        isPrepaymentTv.setText("提前还款：" + bean.getPrepaymentWhether());
        overdueAlgorithmTv.setText("逾期算法：" + bean.getOverdueAlgorithm());
        if (0 == bean.getCreditWhether()){
            isCheckTheCreditTv.setText("查验征信：否");
        } else if (1 == bean.getCreditWhether()) {
            isCheckTheCreditTv.setText("查验征信：是");
        }
        if (0 == bean.getWithdrawalsWhether()) {
            isRaiseOrNotTv.setText("能否提额：否" );
        } else if (1 == bean.getWithdrawalsWhether()) {
            isRaiseOrNotTv.setText("能否提额：是" );
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onDestroy() {
        if (processWeb != null) {
            processWeb.removeAllViews();
            // in android 5.1(sdk:21) we should invoke this to avoid memory leak
            // see (https://coolpers.github.io/webview/memory/leak/2015/07/16/
            // android-5.1-webview-memory-leak.html)
            ((ViewGroup) processWeb.getParent()).removeView(processWeb);
            processWeb.setTag(null);
            processWeb.clearHistory();
            processWeb.destroy();
            processWeb = null;
        }
        super.onDestroy();
    }
}
